var util = require('../../libs/util.js');
//获取应用实例
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addbankcard: true,
    bind: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this;
    //角色切换
    util.getRoleList(this, app.globalData.CO_ID, app.globalData.role_default, app.globalData.roleData);

    wx.showLoading({
      title: '获取数据中...',
    });
    util.getData("/atapi/user/user/invoice", {
      CO_ID: app.globalData.CO_ID
    }, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      if (error) {
        return false;
      }
      if (res.data.success) {
        console.log(res.data.data)
        var data = res.data.data[0];
        that.setData({
          bind: true,
          company: data.company,
          uc_code: data.uc_code,
          bank: data.bank,
          bankcard: data.bankcard,
          tel: data.tel,
          location: data.location
        })
      } else {
        that.setData({
          name: app.globalData.CO_NAME
        })
      }
    });

  },
  reAddBankCard: function () {
    this.setData({
      addbankcard: true
    });
  },
  saveInvoice: function () {
    var that = this;
    var company = that.data.company || '', uc_code = that.data.uc_code || '', bank = that.data.bank || '', bankcard = that.data.bankcard || '', location = that.data.location || '', tel = that.data.tel || '';
    var CO_ID = app.globalData.CO_ID, user_id = app.globalData.user_id;
    if (company == '') {
      wx.showModal({
        title: '温馨提示',
        content: '没有公司名称，请输入公司名称',
        success: function (res) {

        }
      })
      return false;
    }
    if (uc_code == '') {
      wx.showModal({
        title: '温馨提示',
        content: '没有统一信用代码，请输入统一信用代码',
        success: function (res) {

        }
      })
      return false;
    }
    if (bank == '') {
      wx.showModal({
        title: '温馨提示',
        content: '没有开户行，请输入开户行',
        success: function (res) {

        }
      })
      return false;
    }
    if (bankcard == '') {
      wx.showModal({
        title: '温馨提示',
        content: '没有银行卡号信息，请重新上传或输入银行卡号',
        success: function (res) {

        }
      })
      return false;
    }
    if (location == '') {
      wx.showModal({
        title: '温馨提示',
        content: '没有地址，请输入地址',
        success: function (res) {

        }
      })
      return false;
    }
    if (tel == '') {
      wx.showModal({
        title: '温馨提示',
        content: '没有电话，请输入电话',
        success: function (res) {

        }
      })
      return false;
    }
    util.postData("/atapi/user/user/invoice", {
      company: company,
      uc_code:uc_code,
      bank: bank,
      bankcard: bankcard,
      location: location,
      tel: tel,
      CO_ID: CO_ID,
      user_id: user_id
    }, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      if (error) {
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 3000
        });
        return false;
      }
      if (res.data.success) {
        console.log(res.data.data)
        wx.showModal({
          title: '温馨提示',
          content: '发票开票信息设置成功！',
          success: function (res) {
            wx.redirectTo({
              url: '/pages/auth/invoice'
            })
          }
        })
      } else {
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 3000
        });
      }
    });
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})